<template>
  <van-datetime-picker
    v-model="currentDate"
    type="date"
    title="选择年月日"
    :min-date="minDate"
    :max-date="maxDate"
    @confirm="onConfirm"
    @cancel="$emit('close')"
  />
</template>

<script>
import { updateUserProfileAPI } from '../../../api/index.js'
import dayjs from 'dayjs'
export default {
  name: 'BirthdayEdit',
  props: {
    userBirthday: {
      type: String,
      default: ''
    }
  },

  data () {
    return {
      minDate: new Date(1900, 0, 1), // 最小日期
      maxDate: new Date(), // 最大日期
      currentDate: new Date(this.userBirthday) // 当前选中事件
    }
  },

  methods: {
    async onConfirm (value) {
      try {
        await updateUserProfileAPI({
          birthday: dayjs(value).format('YYYY-MM-DD')
        })

        this.$toast.success('修改成功')

        // 更新视图
        this.$emit('update:userBirthday', dayjs(value).format('YYYY-MM-DD'))

        // 关闭弹框
        this.$emit('close')
      } catch (error) {
        console.log(error)
        this.$toast.fail('修改失败')
      }
    }
  }
}
</script>

<style>

</style>
